﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using NJ.Web.ViewModel;
@{
    var ViewModel = Model as RoleMenuViewModel;
    var menus = ViewModel.menus;
    var roles = ViewModel.roles;

}

<form class="layui-form layui-form-pane" action="">
    <div class="layui-form-item">
        <label class="layui-form-label">权限菜单</label>
        <div class="layui-form-mid layui-word-aux">
            @{

                if (menus.Count > 0)
                {
                    <span class="layui-breadcrumb" style="padding-left: 10px;">
                        @foreach (var item in menus)
                        {
                            <a href="javascript:;;">@item.Title</a>
                        }
                    </span>
                }
                else
                {
                    <span class="layui-breadcrumb" style="padding-left: 10px;">
                        <a href="javascript:;;">顶级</a>
                    </span>
                }
            }

        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限角色</label>
        <div class="layui-input-block">
            @foreach (var item in roles)
            {
                <input type="checkbox" name="role_@item.Id" lay-filter="role" lay-skin="primary" value="@item.Id" title="@item.RoleName" @(item.Exist ? "checked" : "")>
            }
        </div>
    </div>

    <input type="hidden" name="menuid" value="@Request.QueryString["Id"]" />

    <div class="layui-form-item" style="display:none">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="save" id="save">立即提交</button>
        </div>
    </div>
</form>

@section style{
    <style>
        .layui-form-checkbox {
            width: 100px;
        }
    </style>
}
@section scripts{
    <script>
        layui.use(['layer', 'element', 'form'], function () {
            var $ = layui.$;
            var layer = parent.layer || layui.layer;
            var form = layui.form;
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            //监听提交
            form.on('submit(save)', function (data) {

                var roleids = [];
                for (var key in data.field) {
                    if (key.indexOf('role_') > -1) {
                        roleids.push(data.field[key]);
                    }
                }
                $.post('/RoleMenu/SaveAsync', {
                    RoleIds: roleids,
                    MenuId: data.field['menuid']
                }, function (res) {
                    if (res.status === 200) {
                        parent.GetMenus();
                        parent.layer.close(index);
                        parent.layer.msg('操作成功', { time: 800 });

                    } else {
                        layer.msg('操作失败', { time: 800 });
                    }
                })
                return false;
            });
            $('#close').click(function () {
                parent.layer.close(index);
            })
        });
    </script>
}